const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/*时间戳处理*/
function getzf(num) {
  if (parseInt(num) < 10) {
    num = '0' + num;
  }
  return num;
}
function getDateNow() {
    var oDate = new Date();
    var oYear = oDate.getFullYear();
    var oMonth = oDate.getMonth() + 1;
    var oDay = oDate.getDate();
    var oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay);//最后拼接时间
    return oTime;
}
/*号码及证件号加密*/
function personTel(Tel) {
  if (Tel==null){
    var phone = '--'
    return phone;
  }else{
    var phone = Tel.replace(Tel.substr(3, Tel.length - 7), '****');
    return phone;
  }
  
}
/**处理数据 */
function keepTwoDecimal(num) {
  var result = parseFloat(num);
  if (isNaN(result)) {
    wx.showToast({
      title: '传递参数错误，请检查！',
    });
    return false;
  }
  result = Math.round(num * 100) / 100;
  return result;
}
function dealRealTimeCommand(cmd) {
  var that = this;
  var list = {};
  var back = parseInt(cmd.substr(16, 4), 16).toString(2);
  back=parseInt(back.substr(4,12),2).toString(10);
  back = keepTwoDecimal(27887 * Math.pow(back, -1.581));
  var waist = parseInt(cmd.substr(12, 4), 16).toString(2);
  waist = parseInt(waist.substr(4, 12), 2).toString(10);
  waist = keepTwoDecimal(19417 * Math.pow(waist, -1.541));
  list.lumbarPressure = waist;
  list.backPressure = back;
  list.measureTime = new Date().toLocaleString();
  return list;
}
//发送数据 方法
function sendMessage(lastTime){
  let timeList = lastTime.split(" ");
  let timeLists = timeList[0].split("-");
  let year = parseInt(timeLists[0], 10).toString(16);
  let month = parseInt(timeLists[1], 10).toString(16);
  let day = parseInt(timeLists[2], 10).toString(16);
  if (year.length != 4) { year = "0" + year; }
  if (month.length != 2) { month = "0" + month; }
  if (day.length != 2) { day = "0" + day; }
  //发送数据
  var sendMesg = "03" + year + month + day; 
  return sendMesg;
}
//处理arraybuffer
function ArrayBuffers(utfstr) {
  var buf = new ArrayBuffer(utfstr.length * 2);
  var bufView = new Uint8Array(buf);
  for (var i = 0, strlen = utfstr.length; i < strlen; i++) {
    bufView[i] = utfstr.charCodeAt(i);
  }
  return buf;
}
module.exports = {
  getDateNow: getDateNow,
  personTel: personTel,
  dealRealTimeCommand: dealRealTimeCommand,
  sendMessage: sendMessage,
  ArrayBuffers:ArrayBuffers
}

